
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileName: '',
    fileSize: '',
    id: '',
    userInfo: {},
    email: '',
    isOver: true,
    isorg: null,
    date1: '',
    date2: '',
    time1: '',
    time2: '',
    showtime: false,
    isall: 0,
    isParentOrgRw:null,
  },
  isallbtn(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    if (id != that.data.isall) {
      that.setData({
        isall: id
      })
      if (that.data.isall == 1) {
        that.setData({
          showtime: true
        })
      }
      if (that.data.isall==0){
        that.setData({
          date1: '',
          date2: '',
          time1: '',
          time2: '',
        })
      }
    }
  },
  bindDateChange1: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date1: e.detail.value
    })
  },
  bindDateChange2: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date2: e.detail.value
    })
  },
  bindTimeChange1: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time1: e.detail.value
    })
  },
  bindTimeChange2: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time2: e.detail.value
    })
  },
  reset() {
    this.setData({
      date1: '',
      date2: '',
      time1: '',
      time2: '',
    })
  },
  confirm() {
    if(this.data.date1==''){
      wx.showToast({
        title: '请设置开始日期',
        icon:'none'
      })
    } else if (this.data.date2 == '') {
      wx.showToast({
        title: '请设置结束日期',
        icon: 'none'
      })
    } else if (this.data.date2 < this.data.date1) {
      wx.showToast({
        title: '结束日期不可早于开始日期',
        icon: 'none'
      })
    }else if (this.data.time1 == '') {
      wx.showToast({
        title: '请设置开始时间',
        icon: 'none'
      })
    } else if (this.data.time2 == '') {
      wx.showToast({
        title: '请设置结束时间',
        icon: 'none'
      })
    } else if (this.data.time2 < this.data.time1) {
      wx.showToast({
        title: '结束时间不可早于开始时间',
        icon: 'none'
      })
    }else{
      this.setData({
        showtime: false,
      })
    }
  }, 
  emailinput(e) {
    this.setData({
      email: e.detail.value.trim()
    })
  },
  svae() {
    var that = this
    var emailPattern = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
    if (that.data.email.length == 0) {
      wx.showToast({
        title: "请输入邮箱地址",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!emailPattern.test(that.data.email)) {
      wx.showToast({
        title: "邮箱格式错误",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (that.data.isOver) {
      that.setData({
        isOver: false,
      })
      var type = null
      if (that.data.isParentOrgRw==0){
        type=2
      }else if(that.data.isParentOrgRw==1){
        type=1
      }
      app.req({
        url: 'orgmini/resumptionWork/exportSecond',
        method: "post",
        data: {
          orgId: that.data.id,
          email: that.data.email,
          type: type,
          startDate: that.data.date1,
          endDate: that.data.date2,
          startTime: that.data.time1 ? that.data.time1 + ':00' : '',
          endTime: that.data.time2 ? that.data.time2 + ':00' : '',
        },
        success: function (res) {
          if (200 == res.code) {
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
              that.setData({
                isOver: true,
              })
            }, 1000)
            wx.showToast({
              title: '导出成功',
              icon: 'none',
              duration: 2000
            })
          } else {
            that.setData({
              isOver: true,
            })
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },
  getname() {
    var that = this
    app.req({
      url: 'orgmini/resumptionWork/exportFirst',
      data: {
        orgId: that.data.id
      },
      success: function (res) {
        if (res.code == 200) {
          that.setData({
            fileName: res.data.fileName
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      id: options.orgid,
      isorg: options.isorg,
      isParentOrgRw: options.isParentOrgRw
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.getname()
    app.getUserInfo(function (userInfo) {
      console.log('用户', userInfo)
      if (userInfo.mail) {
        that.setData({
          email: userInfo.mail
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})